package xin.ganbo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import xin.ganbo.entity.SystemConfig;
import xin.ganbo.service.InitService;
import xin.ganbo.service.SystemConfigService;

import java.util.Date;
import java.util.List;

/**
 *
 * @author ganbo E-mail:bo.gan@56qq.com
 * @since 2017-12-04 10:38
 */
@Service
public class InitServiceImpl implements InitService {

    @Autowired
    private SystemConfigService systemConfigService;

    @Value("${sys.title}")
    private String sysTitle;

    @Value("${sys.name}")
    private String sysName;

    @Value("${sys.version}")
    private String sysVersion;

    @Value("${sys.copyright}")
    private String sysCopyright;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
    @Override
    public void initSysBaseData() {
        initSystemPublishInfo();
    }

    /**
     * 初始化系统发布信息
     */
    public void initSystemPublishInfo() {
        List<SystemConfig> all = systemConfigService.findAll();
        if (CollectionUtils.isEmpty(all)) {
            SystemConfig systemConfig = new SystemConfig();
            systemConfig.setTitle(sysTitle);
            systemConfig.setName(sysName);
            systemConfig.setPublishDate(new Date());
            systemConfig.setSysVersion(sysVersion);
            systemConfig.setCopyright(sysCopyright);
            systemConfigService.save(systemConfig);
        }
    }

}
